package com.example.shudi.sdkdemo.view;

import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.shudi.sdkdemo.R;


public class CustomDialog extends DialogFragment implements View.OnClickListener {
    private static CustomDialog dialog;
    private IClickInterface iClickInterface;
    private TextView tvTakePhoto;
    private TextView tvTakePic;

    public interface IClickInterface {
        void iOnClick(int viewId);
    }

    public static CustomDialog getInstance() {
        if (dialog == null) {
            synchronized (CustomDialog.class) {
                if (dialog == null) {
                    dialog = new CustomDialog();
                }
            }
        }
        return dialog;
    }

    public void setiClickInterface(IClickInterface iClickInterface) {
        this.iClickInterface = iClickInterface;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_dialog, container);
        initView(view);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialogWindow = getDialog();
        if (dialogWindow != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialogWindow.getWindow().setLayout((int) (dm.widthPixels * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    public void initView(View view) {
        tvTakePhoto = view.findViewById(R.id.tv_take_photo);
        tvTakePic = view.findViewById(R.id.tv_take_pic);
        tvTakePhoto.setOnClickListener(this);
        tvTakePic.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_take_photo:
                iClickInterface.iOnClick(R.id.tv_take_photo);
                break;
            case R.id.tv_take_pic:
                iClickInterface.iOnClick(R.id.tv_take_pic);
                break;
        }
    }
}
